package com.king.photo.util;

import java.io.BufferedOutputStream;
import java.io.File;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.IOException;
import java.util.UUID;

import android.content.Context;
import android.graphics.Bitmap;
import android.graphics.Bitmap.CompressFormat;
import android.graphics.BitmapFactory;
import android.graphics.Canvas;
import android.graphics.Paint;
import android.graphics.PorterDuff;
import android.graphics.PorterDuffXfermode;
import android.graphics.RectF;

import com.king.photo.R;

/**
 * Created by 0579A68 on 2015/5/13.
 */
public class BitmapUtil {

	private static BitmapUtil mInstance;

	public static BitmapUtil getInstance() {
		if (mInstance == null) {
			return new BitmapUtil();
		}
		return mInstance;
	}

	public Bitmap toRoundBitmap(Bitmap bitmap, Context context,
			float mCircleCenterX, float mCircleCenterY) {
		int w2 = bitmap.getWidth();
		int h2 = bitmap.getHeight();
		Bitmap backgroundBmp = Bitmap.createBitmap(w2, h2,
				Bitmap.Config.ARGB_8888);
		Bitmap heartBitmap = BitmapFactory.decodeResource(
				context.getResources(), R.drawable.xin);
		Canvas canvas = new Canvas(backgroundBmp);
		Paint paint = new Paint();
		paint.setAntiAlias(true);
		int w = heartBitmap.getWidth() * 2;
		int h = heartBitmap.getHeight() * 2;
		RectF rect2 = new RectF(mCircleCenterX - w / 2, mCircleCenterY - h / 2,
				mCircleCenterX + w / 2, mCircleCenterY + h / 2);
		RectF rect = new RectF(mCircleCenterX - w2 / 2,
				mCircleCenterY - h2 / 2, mCircleCenterX + w2 / 2,
				mCircleCenterY + h2 / 2);

		canvas.drawBitmap(heartBitmap, null, rect2, paint);
		// canvas.drawRoundRect(rect, r/2, r/2, paint);
		paint.setXfermode(new PorterDuffXfermode(PorterDuff.Mode.SRC_IN));
		canvas.drawBitmap(bitmap, null, rect, paint);
		return backgroundBmp;
	}

	/**
	 * 
	 * @param bitmap
	 * @param filePath
	 * @param fileName
	 * @return
	 */
	public static boolean saveBitmapToSDCard(Bitmap bitmap, String filePath,
			String fileName) {
		boolean flag = false;
		if (null != bitmap) {
			try {
				fileName = fileName + ".png";
				File file = new File(filePath);
				if (!file.exists()) {
					file.mkdirs();
				}
				File f = new File(filePath + fileName);
				if (f.exists()) {
					f.delete();
				}
				BufferedOutputStream outputStream = new BufferedOutputStream(
						new FileOutputStream(f));
				bitmap.compress(CompressFormat.PNG, 100, outputStream);
				outputStream.flush();
				outputStream.close();
				flag = true;
			} catch (FileNotFoundException e) {
				flag = false;
			} catch (IOException e) {
				flag = false;
			}
		}
		return flag;
	}

	/**
	 * 保存图片到SD卡
	 * 
	 * @param bitmap
	 *            图片的bitmap对象
	 * @return
	 */
	

}
